package com.file.manage;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public class FileListAdapter extends BaseAdapter {

    private Context mContext;
    private boolean mShow = false;
    private List<CheckEntity> mDataList;
    public FileListAdapter(Context mContext, List<CheckEntity> dataList, boolean show) {
        this.mContext = mContext;
        this.mDataList = dataList;
        this.mShow = show;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return mDataList.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return mDataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.file_list_item, null);
            viewHolder = new ViewHolder();
            viewHolder.fileImage = (ImageView) convertView.findViewById(R.id.file_image);
            viewHolder.fileName = (TextView) convertView.findViewById(R.id.file_name);
            viewHolder.fileSize = (TextView) convertView.findViewById(R.id.file_size);
            viewHolder.modifyDate = (TextView) convertView.findViewById(R.id.time);
            viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.check);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        CheckEntity checkEntity = mDataList.get(position);
        viewHolder.fileName.setText(checkEntity.getFileName().toString());
        viewHolder.fileSize.setText(checkEntity.getFileSize().toString());
        viewHolder.fileImage.setImageResource((Integer) checkEntity.getFileImage());
        viewHolder.modifyDate.setText(checkEntity.getDate().toString());
        if (mShow) {
            viewHolder.checkBox.setVisibility(View.VISIBLE);
            viewHolder.checkBox.setChecked(checkEntity.getSelectCheck());
        } else
            viewHolder.checkBox.setVisibility(View.GONE);
        return convertView;
    }

    private class ViewHolder {

        private TextView fileName;

        private TextView fileSize;

        private ImageView fileImage;

        private TextView modifyDate;

        private CheckBox checkBox;
    }

    public void setmList(List<CheckEntity> dataList, boolean show) {
        this.mDataList = dataList;
        this.mShow = show;
        notifyDataSetChanged();
    }

}
